All files / src/app/(site)/account/orders/[id] page.tsx

0% Statements 0/397
100% Branches 0/0
0% Functions 0/1
0% Lines 0/397

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import { Metadata } from "next";
import { notFound, redirect } from "next/navigation";
import Link from "next/link";
import Image from "next/image";
import { auth } from "@/lib/auth/auth";
import { prisma } from "@/lib/prisma";
import { formatCurrency } from "@/lib/core";
import { Icon } from "@/components/ui/icons";

type Props = {
  params: Promise<{ id: string }>;
};

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { id } = await params;

  return {
    title: `Order #${id} | Elite Events`,
    description: "View order details"};
}

async function OrderDetailPage({ params }: Props) {
  const session = await auth();
  if (!session?.user) {
    redirect("/signin?callbackUrl=/account/orders");
  }

  const { id } = await params;
  const orderId = parseInt(id);

  if (isNaN(orderId)) {
    notFound();
  }

  const order = await prisma.order.findFirst({
    where: {
      id: orderId,
      userId: Number(session.user.id)},
    include: {
      items: {
        include: {
          product: {
            include: {
              images: {
                take: 1,
                orderBy: { order: "asc" }}}}}},
      user: {
        select: { email: true, name: true }}}});

  if (!order) {
    notFound();
  }

  const shippingAddress = JSON.parse(order.shippingAddress || "{}");
  const billingAddress = JSON.parse(order.billingAddress || "{}");

  const formatDate = (date: Date) => {
    return new Date(date).toLocaleDateString("en-US", {
      year: "numeric",
      month: "long",
      day: "numeric",
      hour: "2-digit",
      minute: "2-digit"});
  };

  const getStatusColor = (status: string) => {
    switch (status) {
      case "PROCESSING":
        return "bg-yellow-100 dark:bg-yellow-900/30 text-yellow-700 dark:text-yellow-400 border-yellow-200 dark:border-yellow-700";
      case "SHIPPED":
        return "bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400 border-blue-200 dark:border-blue-700";
      case "DELIVERED":
        return "bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400 border-green-200 dark:border-green-700";
      case "CANCELLED":
        return "bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-400 border-red-200 dark:border-red-700";
      default:
        return "bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 border-gray-200 dark:border-gray-600";
    }
  };

  type IconNameType = "clock" | "truck" | "check-circle" | "x-circle" | "package";

  const getStatusIcon = (status: string): IconNameType => {
    switch (status) {
      case "PROCESSING":
        return "clock";
      case "SHIPPED":
        return "truck";
      case "DELIVERED":
        return "check-circle";
      case "CANCELLED":
        return "x-circle";
      default:
        return "package";
    }
  };

  return (
    <>
      <section className="overflow-hidden pt-[140px] pb-20 bg-gray-2 dark:bg-gray-900">
        <div className="max-w-[1170px] w-full mx-auto px-4 sm:px-8 xl:px-0">
          {/* Back Link */}
          <div className="mb-6">
            <Link
              href="/account/orders"
              className="inline-flex items-center gap-2 text-blue hover:text-blue-dark dark:text-blue-400 dark:hover:text-blue-300 transition-colors"
            >
              <Icon name="arrow-left" size={16} />
              Back to Orders
            </Link>
          </div>

          <div className="grid grid-cols-1 lg:grid-cols-3 gap-7.5">
            {/* Order Details - Left Column */}
            <div className="lg:col-span-2 space-y-7.5">
              {/* Order Info Card */}
              <div className="bg-white dark:bg-gray-800 shadow-1 rounded-[10px] p-4 sm:p-8.5">
                <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between mb-6">
                  <h1 className="font-bold text-2xl text-dark dark:text-white mb-2 sm:mb-0">
                    Order #{order.id}
                  </h1>
                  <div
                    className={`inline-flex items-center gap-2 px-4 py-2 rounded-full border ${getStatusColor(order.status)} text-sm font-medium`}
                  >
                    <Icon name={getStatusIcon(order.status)} size={16} />
                    <span className="capitalize">{order.status.toLowerCase()}</span>
                  </div>
                </div>

                <div className="grid grid-cols-2 sm:grid-cols-3 gap-4 text-sm">
                  <div>
                    <p className="text-gray-5 dark:text-gray-400 mb-1">Order Date</p>
                    <p className="font-medium text-dark dark:text-gray-200">
                      {formatDate(order.createdAt)}
                    </p>
                  </div>
                  <div>
                    <p className="text-gray-5 dark:text-gray-400 mb-1">Total Items</p>
                    <p className="font-medium text-dark dark:text-gray-200">
                      {order.items.reduce((sum, item) => sum + item.quantity, 0)} items
                    </p>
                  </div>
                  <div>
                    <p className="text-gray-5 dark:text-gray-400 mb-1">Email</p>
                    <p className="font-medium text-dark dark:text-gray-200">
                      {order.user?.email}
                    </p>
                  </div>
                </div>
              </div>

              {/* Order Items */}
              <div className="bg-white dark:bg-gray-800 shadow-1 rounded-[10px] p-4 sm:p-8.5">
                <h2 className="font-medium text-xl text-dark dark:text-white mb-5">
                  Order Items ({order.items.length} {order.items.length === 1 ? "product" : "products"})
                </h2>
                <div className="space-y-4">
                  {order.items.map((item) => {
                    const thumbnail = item.product.images[0]?.thumbnailUrl || item.product.images[0]?.url;

                    return (
                      <div
                        key={item.id}
                        className="flex gap-4 p-4 border border-gray-200 dark:border-gray-700 rounded-lg"
                      >
                        {/* Product Image */}
                        <div className="w-20 h-20 flex-shrink-0 bg-gray-100 dark:bg-gray-700 rounded-lg overflow-hidden">
                          {thumbnail ? (
                            <Image
                              src={thumbnail}
                              alt={item.product.title}
                              width={80}
                              height={80}
                              className="w-full h-full object-cover"
                            />
                          ) : (
                            <div className="w-full h-full flex items-center justify-center">
                              <Icon name="package" size={32} className="text-gray-400" />
                            </div>
                          )}
                        </div>

                        {/* Product Info */}
                        <div className="flex-1 min-w-0">
                          <Link
                            href={`/product/${item.product.id}`}
                            className="font-medium text-dark dark:text-white hover:text-blue dark:hover:text-blue-400 transition-colors line-clamp-2"
                          >
                            {item.product.title}
                          </Link>
                          <div className="mt-2 flex flex-wrap items-center gap-x-2 gap-y-1 text-sm text-gray-5 dark:text-gray-400">
                            <span>Qty: {item.quantity}</span>
                            <span className="text-gray-300 dark:text-gray-600">|</span>
                            <span>{formatCurrency(item.price)} each</span>
                          </div>
                        </div>

                        {/* Item Total */}
                        <div className="text-right">
                          <p className="font-bold text-dark dark:text-white">
                            {formatCurrency(item.price * item.quantity)}
                          </p>
                        </div>
                      </div>
                    );
                  })}
                </div>
              </div>

              {/* Addresses */}
              <div className="grid grid-cols-1 sm:grid-cols-2 gap-7.5">
                {/* Shipping Address */}
                <div className="bg-white dark:bg-gray-800 shadow-1 rounded-[10px] p-4 sm:p-8.5">
                  <div className="flex items-center gap-2 mb-4">
                    <Icon name="truck" size={20} className="text-blue" />
                    <h3 className="font-medium text-lg text-dark dark:text-white">
                      Shipping Address
                    </h3>
                  </div>
                  <address className="not-italic text-gray-5 dark:text-gray-400 space-y-1 text-sm">
                    <p className="font-medium text-dark dark:text-gray-200">{shippingAddress.name || order.user?.name}</p>
                    <p>{shippingAddress.street}</p>
                    <p>
                      {shippingAddress.city}, {shippingAddress.state}{" "}
                      {shippingAddress.zipCode}
                    </p>
                    <p>{shippingAddress.country}</p>
                  </address>
                </div>

                {/* Billing Address */}
                <div className="bg-white dark:bg-gray-800 shadow-1 rounded-[10px] p-4 sm:p-8.5">
                  <div className="flex items-center gap-2 mb-4">
                    <Icon name="credit-card" size={20} className="text-green-500" />
                    <h3 className="font-medium text-lg text-dark dark:text-white">
                      Billing Address
                    </h3>
                  </div>
                  <address className="not-italic text-gray-5 dark:text-gray-400 space-y-1 text-sm">
                    <p className="font-medium text-dark dark:text-gray-200">{billingAddress.name || order.user?.name}</p>
                    <p>{billingAddress.street}</p>
                    <p>
                      {billingAddress.city}, {billingAddress.state}{" "}
                      {billingAddress.zipCode}
                    </p>
                    <p>{billingAddress.country}</p>
                  </address>
                </div>
              </div>
            </div>

            {/* Order Summary - Right Column */}
            <div className="lg:col-span-1">
              <div className="bg-white dark:bg-gray-800 shadow-1 rounded-[10px] p-4 sm:p-8.5 sticky top-20">
                <h3 className="font-medium text-xl text-dark dark:text-white mb-5">
                  Order Summary
                </h3>

                {/* Subtotal */}
                <div className="py-3 border-b border-gray-3 dark:border-gray-700 flex justify-between">
                  <p className="text-gray-5 dark:text-gray-400">
                    Subtotal ({order.items.reduce((sum, item) => sum + item.quantity, 0)} items)
                  </p>
                  <p className="font-medium text-dark dark:text-gray-200">
                    {formatCurrency(
                      order.items.reduce((sum, item) => sum + item.price * item.quantity, 0)
                    )}
                  </p>
                </div>

                {/* Discount (if applicable) */}
                {order.discountAmount != null && order.discountAmount > 0 ? (
                  <div className="py-3 border-b border-gray-3 dark:border-gray-700 flex justify-between">
                    <p className="text-gray-5 dark:text-gray-400">
                      Discount {order.appliedPromoCode ? <span className="text-xs">({order.appliedPromoCode})</span> : null}
                    </p>
                    <p className="font-medium text-green-600 dark:text-green-400">
                      -{formatCurrency(order.discountAmount)}
                    </p>
                  </div>
                ) : null}

                {/* Shipping */}
                <div className="py-3 border-b border-gray-3 dark:border-gray-700 flex justify-between">
                  <p className="text-gray-5 dark:text-gray-400">Shipping</p>
                  <p className="font-medium text-green-600 dark:text-green-400">Free</p>
                </div>

                {/* Total */}
                <div className="py-4 flex justify-between">
                  <p className="font-bold text-lg text-dark dark:text-white">Total</p>
                  <p className="font-bold text-lg text-blue dark:text-blue-400">
                    {formatCurrency(order.total)}
                  </p>
                </div>

                {/* Order Timeline */}
                <div className="mt-6 pt-6 border-t border-gray-3 dark:border-gray-700">
                  <h4 className="font-medium text-dark dark:text-white mb-4">Order Status</h4>
                  <div className="space-y-3">
                    <div className="flex items-center gap-3">
                      <div className="w-8 h-8 rounded-full bg-green-100 dark:bg-green-900/30 flex items-center justify-center">
                        <Icon name="check" size={16} className="text-green-600 dark:text-green-400" />
                      </div>
                      <div>
                        <p className="text-sm font-medium text-dark dark:text-gray-200">Order Placed</p>
                        <p className="text-xs text-gray-5 dark:text-gray-400">{formatDate(order.createdAt)}</p>
                      </div>
                    </div>

                    <div className="flex items-center gap-3">
                      <div className={`w-8 h-8 rounded-full flex items-center justify-center ${
                        ["PROCESSING", "SHIPPED", "DELIVERED"].includes(order.status)
                          ? "bg-green-100 dark:bg-green-900/30"
                          : "bg-gray-100 dark:bg-gray-700"
                      }`}>
                        <Icon
                          name={["PROCESSING", "SHIPPED", "DELIVERED"].includes(order.status) ? "check" : "clock"}
                          size={16}
                          className={["PROCESSING", "SHIPPED", "DELIVERED"].includes(order.status) ? "text-green-600 dark:text-green-400" : "text-gray-400"}
                        />
                      </div>
                      <div>
                        <p className="text-sm font-medium text-dark dark:text-gray-200">Processing</p>
                        <p className="text-xs text-gray-5 dark:text-gray-400">
                          {order.status === "PROCESSING" ? "In progress" : ["SHIPPED", "DELIVERED"].includes(order.status) ? "Completed" : "Pending"}
                        </p>
                      </div>
                    </div>

                    <div className="flex items-center gap-3">
                      <div className={`w-8 h-8 rounded-full flex items-center justify-center ${
                        ["SHIPPED", "DELIVERED"].includes(order.status)
                          ? "bg-green-100 dark:bg-green-900/30"
                          : "bg-gray-100 dark:bg-gray-700"
                      }`}>
                        <Icon
                          name={["SHIPPED", "DELIVERED"].includes(order.status) ? "check" : "truck"}
                          size={16}
                          className={["SHIPPED", "DELIVERED"].includes(order.status) ? "text-green-600 dark:text-green-400" : "text-gray-400"}
                        />
                      </div>
                      <div>
                        <p className="text-sm font-medium text-dark dark:text-gray-200">Shipped</p>
                        <p className="text-xs text-gray-5 dark:text-gray-400">
                          {order.status === "SHIPPED" ? "On the way" : order.status === "DELIVERED" ? "Completed" : "Pending"}
                        </p>
                      </div>
                    </div>

                    <div className="flex items-center gap-3">
                      <div className={`w-8 h-8 rounded-full flex items-center justify-center ${
                        order.status === "DELIVERED"
                          ? "bg-green-100 dark:bg-green-900/30"
                          : "bg-gray-100 dark:bg-gray-700"
                      }`}>
                        <Icon
                          name={order.status === "DELIVERED" ? "check" : "package"}
                          size={16}
                          className={order.status === "DELIVERED" ? "text-green-600 dark:text-green-400" : "text-gray-400"}
                        />
                      </div>
                      <div>
                        <p className="text-sm font-medium text-dark dark:text-gray-200">Delivered</p>
                        <p className="text-xs text-gray-5 dark:text-gray-400">
                          {order.status === "DELIVERED" ? "Completed" : "Pending"}
                        </p>
                      </div>
                    </div>
                  </div>
                </div>

                {/* Action Buttons */}
                <div className="space-y-3 mt-6">
                  <Link
                    href="/account/orders"
                    className="block w-full text-center bg-blue text-white py-2.5 rounded-md font-medium hover:bg-blue-dark dark:hover:bg-blue-600 transition-colors"
                  >
                    Back to Orders
                  </Link>
                  <Link
                    href="/shop"
                    className="block w-full text-center bg-gray-1 dark:bg-gray-700 text-dark dark:text-gray-200 py-2.5 rounded-md font-medium hover:bg-gray-3 dark:hover:bg-gray-600 transition-colors border border-gray-3 dark:border-gray-600"
                  >
                    Continue Shopping
                  </Link>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
    </>
  );
}

export default OrderDetailPage;